package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.UserService;
import cn.huiyunche.base.service.interfaces.WaybillBidService;
import cn.huiyunche.base.service.mappers.CWaybillBidMapper;
import cn.huiyunche.base.service.model.CWaybillBid;
import cn.huiyunche.base.service.model.CWaybillBidExample;
import cn.huiyunche.base.service.vo.UserVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WaybillBidServiceImpl implements WaybillBidService {

    @Autowired
    private CWaybillBidMapper waybillBidMapper;

    @Autowired
    private UserService userService;

    @Override
    public Long addWayBillBid(Long orderId, Long waybillId, Long driverId, boolean b) throws Exception {
        if (orderId == null || orderId == 0) {
            throw new BusinessException("订单id不能为空！");
        }
        if (waybillId == null || waybillId == 0) {
            throw new BusinessException("运单id不能为空！");
        }

        UserVo user = userService.getCurrentUserVo();

        CWaybillBid bid = new CWaybillBid();
        bid.setOrderId(orderId);
        bid.setWaybillId(waybillId);
        if (null != driverId) {
            bid.setActDriver(driverId);
        } else {
            bid.setActDriver(user.getId());
        }
        bid.setBidderId(user.getId());
        bid.setBidderType(user.getType());
        bid.setStatus(b);
        waybillBidMapper.insertSelective(bid);

        return bid.getId();
    }

    @Override
    public CWaybillBid selectByOrderid(long orderId) {
        CWaybillBid cWaybillBid = null;
        try {
            CWaybillBidExample example = new CWaybillBidExample();
            example.createCriteria().andOrderIdEqualTo(orderId);
            List<CWaybillBid> list = waybillBidMapper.selectByExample(example);
            if (list.size() > 0) {
                cWaybillBid = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cWaybillBid;
    }

    @Override
    public Long insertCWaybillBid(CWaybillBid bid) {
        try {
            waybillBidMapper.insertSelective(bid);
        } catch (Exception e) {
            throw new BusinessException("添加竞单记录异常");
        }
        return bid.getId();
    }

}
